package com.ifp.opengate.boot.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Component;

import com.ifp.core.log.Trace;
import com.ifp.opengate.boot.emum.ErrorEnum;

/**
 * 
 * @author gylio
 *
 */
@Component("RouteBlackBeanUtils")
public class RouteBlackBeanUtils {

    private String charset = "UTF-8";

    private ConcurrentHashMap<Object, Object> routeBlackParamMap = new ConcurrentHashMap();

    public ConcurrentHashMap<Object, Object> getRouteBlackParamMap() {
        return routeBlackParamMap;
    }

    public void setRouteBlackParamMap(ConcurrentHashMap<Object, Object> routeBlackParamMap) {
        this.routeBlackParamMap = routeBlackParamMap;
    }

    public void setRouteBlackInfo(Object key, Object value) {
        routeBlackParamMap.put(key, value);
    }

    public String removeRouteBlack(Object grbCode) {

        try {
            routeBlackParamMap.remove(grbCode);
            Trace.logError(Trace.MODULE_CORE, "路由黑名单缓存{}清除成功", grbCode);
            return ErrorEnum.success.code();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Trace.logError(Trace.MODULE_CORE, "路由黑名单缓存{}清除异常", grbCode);
            return ErrorEnum.error.code();
        }

    }

    public String initOneRouteBlack(Map<Object, Object> route_black_map) {

        try {
            routeBlackParamMap.clear();// 重新整理
            for (Object key : route_black_map.keySet()) {
                Object value = route_black_map.get(key).toString();
                setRouteBlackInfo(key, value);
            }
            Trace.logInfo(Trace.MODULE_CORE, "路由黑名单缓存{}添加成功", route_black_map);
            return ErrorEnum.success.code();
        } catch (Exception e) {
            Trace.logError(Trace.MODULE_CORE, "路由黑名单缓存{}添加异常", route_black_map);
            return ErrorEnum.error.code();
        }

    }
}
